package lib.bone.system.extend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.system.extend.dao.SysDictDataDAO;
import lib.bone.system.extend.dao.SysDictTypeDAO;
import lib.bone.system.extend.domain.dto.DictDataAddDTO;
import lib.bone.system.extend.domain.dto.DictDataUpdateDTO;
import lib.bone.system.extend.domain.dto.DictTypeAddDTO;
import lib.bone.system.extend.domain.dto.DictTypeUpdateDTO;
import lib.bone.system.extend.domain.entity.SysDictData;
import lib.bone.system.extend.domain.entity.SysDictType;
import lib.bone.system.extend.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Slf4j
@Service
public class SysDictServiceImpl implements SysDictService {
    @Autowired
    SysDictDataDAO sysDictDataDAO;

    @Autowired
    SysDictTypeDAO sysDictTypeDAO;

    @Override
    public List<SysDictData> findByDictType(String dictType) {
        return sysDictDataDAO.findByDictTypeOrderByDictSortDesc(dictType);
    }

    public Optional<SysDictType> getDictType(Long dictId){
        return sysDictTypeDAO.findById(dictId);
    }
    public ResponseDTO deleteDictType(Long dictId){
        sysDictTypeDAO.deleteById(dictId);
        return ResponseDTO.succMsg("删除成功!");
    }
    public ResponseDTO updateDictType(DictTypeUpdateDTO updateDTO){
        Long dictId = updateDTO.getId();
        Optional<SysDictType> sysDictTypeOp = getDictType(dictId);
        if(!sysDictTypeOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        SysDictType sysDictType = new SysDictType();
        BeanUtil.copyProperties(updateDTO,sysDictType,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(sysDictTypeDAO.save(sysDictType));
    }
    public ResponseDTO saveDictType(DictTypeAddDTO addDTO){
        SysDictType sysDictType = new SysDictType();
        Optional<SysDictType> existOp = sysDictTypeDAO.findByDictNameAndDictType(addDTO.getDictName(), addDTO.getDictType());
        if(existOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.FORBIDDEN);
        BeanUtil.copyProperties(addDTO,sysDictType,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(sysDictTypeDAO.save(sysDictType));
    }

    @Override
    public ResponseDTO<PageResultDTO<SysDictType>> getDictTypes(Pageable pageable, Example<SysDictType> example) {
        Page<SysDictType> page;
        //是否为空 --
        if(Validator.isEmpty(example)){
            page = sysDictTypeDAO.findAll(pageable);
        }else {
            page = sysDictTypeDAO.findAll(example,pageable);
        }
        List<SysDictType> list = page.getContent();
        PageResultDTO<SysDictType> pageResultDTO = new PageResultDTO<>();
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());

        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public ResponseDTO saveDictData(DictDataAddDTO dictDataAddDTO) {
        /**
         * 判断是否存在 sys_dict_type
         */
        Optional<SysDictType> sysDictTypeOp = sysDictTypeDAO.findByDictType(dictDataAddDTO.getDictType());
        if(!sysDictTypeOp.isPresent()){
            //不存在则新建
            SysDictType sysDictType = new SysDictType();
            BeanUtil.copyProperties(dictDataAddDTO,sysDictType, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            sysDictTypeDAO.save(sysDictType);
        }
        /**
         * 判断是否存在 sys_dict_data
         */
        SysDictData sysDictData = new SysDictData();
        Optional<SysDictData> sysDictDataOp = sysDictDataDAO.findByDictLabelAndDictValueAndDictType(
                dictDataAddDTO.getDictLabel(),
                dictDataAddDTO.getDictValue(),
                dictDataAddDTO.getDictType()
        );
        // BUG fix 2021-12-30 判断逻辑修复
        if(!sysDictDataOp.isPresent()){
            BeanUtil.copyProperties(dictDataAddDTO,sysDictData,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
            return ResponseDTO.succData(sysDictDataDAO.save(sysDictData));
        }else {
            return ResponseDTO.wrap(ResponseCodeConst.ACCEPTED,"重复数据！");
        }
    }

    public Optional<SysDictData> getDictData(Long dataId){
        return sysDictDataDAO.findById(dataId);
    }

    public ResponseDTO updateDictData(DictDataUpdateDTO updateDTO){
        Long id = updateDTO.getId();
        Optional<SysDictData> sysDictDataOp = getDictData(id);
        if(!sysDictDataOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        SysDictData sysDictData = new SysDictData();
        BeanUtil.copyProperties(updateDTO,sysDictData,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        return ResponseDTO.succData(sysDictDataDAO.save(sysDictData));
    }

    public ResponseDTO deleteDictData(Long dataId){
        sysDictDataDAO.deleteById(dataId);
        return ResponseDTO.succMsg("删除成功!");
    }
}
